package com.example.mapper.mapper;

import java.util.Map;
import org.apache.ibatis.jdbc.SQL;

public class UserSqlProvider {

    private final static String TABLE_NAME = "user";

    //获取用户列表
    public String getList(Map<String,Object> params){
        int page = (int)params.get("page");
        int pageSize = (int)params.get("pageSize");
        String q = (String) params.get("q");
        String username = (String) params.get("username");
        String name = (String) params.get("name");
        String age = (String) params.get("age");

        String sql = new SQL(){{
            SELECT("*");
            FROM(TABLE_NAME);
            if(params.get("q")!=null){
                WHERE("name LIKE #{q} OR username LIKE #{q}");
            }
            if (params.get("username")!=null){
                WHERE("username LIKE #{username}");
            }
            if(params.get("name")!=null){
                WHERE("name LIKE #{name}");
            }
            if(params.get("age")!=null){
                WHERE("age = #{age}");
            }
        }}.toString();
        System.out.println(sql.trim()+" limit "+(page-1)*pageSize+","+pageSize);
        return sql.trim()+" limit "+(page-1)*pageSize+","+pageSize;
    }

}
